package com.baosight.sczx.common.zm.cInterface.cf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cf.domain.ZMCFMessageCache;

public class MessageUtil 
{
	private Dao dao;
	
	public void setDao(Dao dao)
	{
		this.dao = dao;
	}
	/**
	 * 
	 * <p>Function: insertIntoMessageCache</p>
	 * <p>Description: 消息插入消息缓存区</p>
	 * <p>Parameter1:(String)inMessageTitle 消息标题  必填</p>
	 * <p>Parameter2:(String)inPageCode 页面号  必填</p>
	 * <p>Parameter3:(String)inDepartmentId 部门ID 可为""</p>
	 * <p>Parameter4:(String)inUserId 用户ID  可为""</p>
	 * <p>Parameter5:(int)filterType 消息发送过滤类型  仅可为0、1、2、3、4
	 * 		0：发给部门所有用户；
	 * 		1：发给部门中有某页面权限用户，此时页面号必填；
	 * 		2：发给有某页面权限的所有用户，此时页面号必填；
	 * 		3：发给系统中所有用户；
	 * 		4：发给某个具体用户，此时用户ID必填；
	 * </p>
	 * <p>return value:int
	 * 		-1:传入参数有误;
	 * 		 0:消息发送有误;
	 * 		 1:消息发送正常;
	 * </p>
	 * <p>History:
	 *    2011-8-18下午02:10:50 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int insertIntoMessageCache(String inMessageTitle, 
									String inPageCode, 
									String inDepartmentId,
									String inUserId,
									int filterType)
	{
		if(inMessageTitle == null || inMessageTitle.equals(""))
		{
			return -1;
		}
		
		if(inPageCode == null || inPageCode.equals(""))
		{
			return -1;
		}
		
		if(filterType>4 || filterType < 0)
		{
			return -1;
		}
		
		Map inMap = new HashMap();
		
		String keyId = TablePk.getInstance().getNextValue("T_CF_MESSAGECACHE");
		inMap.put("fdKeyId", keyId);
		if(inMessageTitle.length() > 20)
		{
			inMap.put("fdAlias", inMessageTitle.substring(0, 20)+"...");
		}
		else
		{
			inMap.put("fdAlias", inMessageTitle);
		}
		
		inMap.put("fdBusinessId", "default");
		inMap.put("fdMessageType", "系统消息");
		inMap.put("fdMessageTitle", "[" + StaticTools.getDate() + "]"+inMessageTitle);
		inMap.put("fdFrontPage", inPageCode);
		inMap.put("fdSendTime", StaticTools.getDateTime());
		inMap.put("fdFilterType", filterType);
		inMap.put("fdDepartmentId", inDepartmentId);
		if(inUserId.equals(""))
		{
			inUserId = "FD_USER_ID";
		}
		
		inMap.put("fdUserId", inUserId);
		
		try
		{
			dao.insert("ZMCFMessageCache.insert", inMap);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return 0;
		}
		
		return 1;
	}
	
	/**
	 * 
	 * <p>Function: DecomposeMessage</p>
	 * <p>Description: 分解消息</p>
	 * <p>return value:int
	 * 		-1:传入参数有误;
	 * 		 0:消息发送有误;
	 * 		 1:消息发送正常;
	 * <p>History:
	 *    2011-8-18下午05:26:02 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int decomposeMessage()
	{
		try
		{
			List lsCache = dao.query("ZMCFMessageCache.query", new HashMap());
			
			for(int i=0; i<lsCache.size(); i++)
			{
				ZMCFMessageCache cache = (ZMCFMessageCache)lsCache.get(i);
	
				int returnValue = sendMessage(cache.getFdMessageTitle(), cache.getFdBusinessId(),
						cache.getFdMessageType(), cache.getFdFrontPage(),
						cache.getFdDepartmentId(), cache.getFdUserId(),
						Integer.parseInt(cache.getFdFilterType()));
				
				if(returnValue != 1)
				{
					return returnValue;
				}
			}
			
			for(int i=0; i<lsCache.size(); i++)
			{
				ZMCFMessageCache cache = (ZMCFMessageCache)lsCache.get(i);
				Map inMap = new HashMap();
				inMap.put("fdSendStatus", 1);
				inMap.put("fdKeyId", cache.getFdKeyId());
				dao.update("ZMCFMessageCache.update", inMap);
			}
			
			delete();
			return 1;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 
	 * <p>Function: sendMessage</p>
	 * <p>Description: 发送消息</p>
	 * <p>Parameter1:(String)inMessageTitle 消息标题  必填</p>
	 * <p>Parameter2:(String)inBusinessId 业务类型  必填</p>
	 * <p>Parameter3:(String)inMessageType 消息类型  必填</p>
	 * <p>Parameter4:(String)inPageCode 页面号  可为""</p>
	 * <p>Parameter5:(String)inDepartmentId 部门ID 必填</p>
	 * <p>Parameter6:(String)inUserId 用户ID  可为""</p>
	 * <p>Parameter7:(int)filterType 消息发送过滤类型  仅可为0、1、2、3、4
	 * 		0：发给部门所有用户；
	 * 		1：发给部门中有某页面权限用户，此时页面号必填；
	 * 		2：发给有某页面权限的所有用户，此时页面号必填；
	 * 		3：发给系统中所有用户；
	 * 		4：发给某个具体用户，此时用户ID必填；
	 * </p>
	 * <p>return value:int
	 * 		-1:传入参数有误;
	 * 		 0:消息发送有误;
	 * 		 1:消息发送正常;
	 * </p>
	 * <p>History:
	 *    2011-8-18下午02:10:50 bobswang
	 * </p>
	 *
	 */
	private int sendMessage(String inMessageTitle, 
									String inBusinessId,
									String inMessageType,
									String inPageCode, 
									String inDepartmentId,
									String inUserId,
									int filterType)
	{
		int returnValue = 0;
		
		if(filterType>4 || filterType < 0)
		{
			return -1;
		}
		
		switch(filterType)
		{
			case 0:
				if(inDepartmentId == null || inDepartmentId.equals(""))
				{
					returnValue = -1;
					break;
				}
				else
				{
					boolean result = insertMessageToDataBase(inMessageTitle, inBusinessId,
							inMessageType, inPageCode, inDepartmentId, 
							queryUser(inDepartmentId, ""));
					
					if(result)
					{
						returnValue = 1;
					}
					else
					{
						returnValue = 0;
					}
					break;
				}
			case 1:
				if(inPageCode == null || inPageCode.equals(""))
				{
					returnValue = -1;
					break;
				}
				if(inDepartmentId == null || inDepartmentId.equals(""))
				{
					returnValue = -1;
					break;
				}
				
				boolean result = insertMessageToDataBase(inMessageTitle, inBusinessId,
						inMessageType, inPageCode, inDepartmentId, 
						queryUserWithPageCode(inPageCode, inDepartmentId));
				if(result)
				{
					returnValue = 1;
				}
				else
				{
					returnValue = 0;
				}
				
				break;
			case 2:
				if(inPageCode == null || inPageCode.equals(""))
				{
					returnValue = -1;
					break;
				}
				
				result = insertMessageToDataBase(inMessageTitle, inBusinessId,
						inMessageType, inPageCode, inDepartmentId, 
						queryUserWithPageCode(inPageCode, ""));
				if(result)
				{
					returnValue = 1;
				}
				else
				{
					returnValue = 0;
				}
				
				break;
			case 3:
				result = insertMessageToDataBase(inMessageTitle, inBusinessId,
						inMessageType, inPageCode, inDepartmentId, 
						queryUser("", ""));
				if(result)
				{
					returnValue = 1;
				}
				else
				{
					returnValue = 0;
				}
				
				break;
			case 4:
				if(inUserId == null || inUserId.equals(""))
				{
					returnValue = -1;
					break;
				}
				
				result = insertMessageToDataBase(inMessageTitle, inBusinessId,
						inMessageType, inPageCode, inDepartmentId, 
						queryUser("", inUserId));
				if(result)
				{
					returnValue = 1;
				}
				else
				{
					returnValue = 0;
				}
				
				break;
			default:
				break;
		}
		
		return returnValue;
	}
	
	/**
	 * 
	 * <p>Function: insertMessageToDataBase</p>
	 * <p>Description: </p>
	 * <p>Parameter1:(String)inMessageTitle 消息标题  必填</p>
	 * <p>Parameter2:(String)inBusinessId 业务类型  必填</p>
	 * <p>Parameter3:(String)inMessageType 消息类型  必填</p>
	 * <p>Parameter4:(String)inPageCode 页面号  可为""</p>
	 * <p>Parameter5:(String)inDepartmentId 部门ID 必填</p>	 
	 * <p>Parameter6:(List)inLsUsers 用户ID List</p>
	 * <p>return value:boolean</p>
	 * <p>History:
	 *    2011-8-18下午05:22:57 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private boolean insertMessageToDataBase(String inMessageTitle, 
									String inBusinessId,
									String inMessageType,
									String inPageCode, 
									String inDepartmentId,
									List inLsUsers)
	{
		boolean returnValue = true;
		try
		{
			for(int i=0; i<inLsUsers.size(); i++)
			{
				Map _map = (HashMap)inLsUsers.get(i);
				Map inMap = new HashMap();
				
				String keyId = TablePk.getInstance().getNextValue("T_CF_MESSAGELOG");
				String userId = (String)_map.get("fdName");
				inMap.put("fdKeyId", keyId);
				inMap.put("fdUserId", userId);
				inMap.put("fdMessageId", "default");
				inMap.put("fdBusinessId", inBusinessId);
				inMap.put("fdMessageType", inMessageType);
				inMap.put("fdMessageTitle", inMessageTitle);
				inMap.put("fdFrontType", "DETAILS");
				inMap.put("fdFrontPage", inPageCode);
				inMap.put("fdSendTime", StaticTools.getDateTime());
				inMap.put("fdStatus", "0");
				
				dao.insert("ZMCFMessageLog.insert", inMap);
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			returnValue =  false;
		}
		
		return returnValue;
	}
	
	/**
	 * 
	 * <p>Function: queryUser</p>
	 * <p>Description: 查询某部门的/所有部门的/特定的用户</p>
	 * <p>return value:List</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-18下午03:02:28 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List queryUser(String inDepartmentId, String inUserId)
	{
		Map inMap = new HashMap();
		
		if(inDepartmentId != null && !inDepartmentId.equals(""))
		{
			inMap.put("fdOrgCode", inDepartmentId);
		}
		if(inUserId != null && !inUserId.equals(""))
		{
			inMap.put("fdName", inUserId);
		}

		return dao.query("ZMCFMessageCache.queryUser", inMap);
	}
	
	/**
	 * 
	 * <p>Function: queryUserWithPageCode</p>
	 * <p>Description: 查询某页面在某部门/所有部门中具有权限的用户</p>
	 * <p>return value:List</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-18下午03:02:46 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List queryUserWithPageCode(String inPageCode, String inDepartmentId)
	{
		Map inMap = new HashMap();
		inMap.put("fdObjectType", "TYP_PAGE");
		inMap.put("fdOpt", "OPT_ES00");
		inMap.put("fdObjectName", inPageCode);
		
		if(inDepartmentId != null && !inDepartmentId.equals(""))
		{
			inMap.put("fdOrgCode", inDepartmentId);
		}
		
		return dao.query("ZMCFMessageCache.queryAuthUser", inMap);
	}
	
	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 按状态删除缓存</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2011-8-18下午05:43:22 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void delete()
	{
		Map inMap = new HashMap();
		inMap.put("fdSendStatus", 1);

		dao.delete("ZMCFMessageCache.delete", inMap);
	}
	
	/**构造是或者否block
	 * <p>Function: getTrueOrFalse</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-7-3 上午11:01:49  高强 
	 * </p>
	 *
	*/
	@SuppressWarnings("rawtypes")
	public static EiBlock  getIsTrueOrFalse(String blockName)
	{
		EiBlock block = new EiBlock(blockName);

		EiColumn eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map>  _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		
		_map = new HashMap<String, String>();
		_map.put("display", "是");
		_map.put("value", "是");
        _list.add(_map);
        
        _map = new HashMap<String, String>();
		_map.put("display", "否");
		_map.put("value", "否");
        _list.add(_map);
        
		return block;
	
	}
}
